/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.utils
{
	/**
	 * MathUtils
	 * Utilities for mathematical calculations
	 * 
	 * @langversion ActionScript 3
	 * 
	 * @author M.P. Bartnig
	 * @since  16/03/2009
	 */
	public class MathUtils
	{
		/** Converts degrees to radians 
		* 	@param 	degrees 		Number. The angle in degrees.
		* 	@return					Number. The angle in radians.
		*/
		public static function toRadians( degrees:Number ):Number
		{
			return ( ( Math.PI / 180 ) * degrees );
		}
		
		/** Converts radians to degrees
		* 	@param 	radians 		Number. The angle in radians.
		* 	@return					Number. The angle in degrees.
		*/
		public static function toDegrees( radians:Number ):Number
		{
			return ( ( 180 / Math.PI ) * radians );
		}
		
		/** Returns the horizontal part of a vector, using total vector length and angle in degrees 
		* 	@param 	vectorTotal 	Number.
		* 	@param 	angle 			Number. The angle in degrees of the specified vector.
		* 	@return					Number. The horizontal part of the vector.
		*/
		public static function getXVector( vectorTotal:Number, angle:Number ):Number
		{
			//return ( Math.sin ( toRadians ( Math.abs ( angle ) ) ) * vectorTotal );
			return ( Math.cos( toRadians( angle ) ) * vectorTotal );
		}
		
		/** Returns the vertical part of a vector, using total vector length and angle in degrees 
		* 	@param 	vectorTotal 	Number.
		* 	@param 	angle 			Number. The angle in degrees of the specified vector.
		* 	@return					Number. The vertical part of the vector.
		*/
		public static function getYVector ( vectorTotal:Number, degrees:Number ):Number
		{
			return ( Math.sin( toRadians( degrees ) ) * vectorTotal );
		}
		
		
		/** Returns the distance between 2 points
		* 	@param 	x1 				Number. The x-position of the startpoint
		* 	@param 	y1 				Number. The y-position of the startpoint
		* 	@param 	x2 				Number. The x-position of the endpoint
		* 	@param 	y2 				Number. The y-position of the endpoint
		* 	@return					Number. The distance between 2 points.
		*/
		public static function getDistanceBetweenPoints( x1:Number, y1:Number, x2:Number, y2:Number ):Number
		{
			var distanceX:Number	= x2 - x1;
			var distanceY:Number	= y2 - y1;
			
			return ( getDistanceFromVectors( distanceX, distanceY ) );
		}
		
		/** Returns the distance between 2 points based on x and y vectors
		* 	@param 	dx 				Number. The x-position of the startpoint
		* 	@param 	dy 				Number. The y-position of the startpoint
		* 	@return					Number. The distance between 2 points based on x and y vectors.
		*/
		public static function getDistanceFromVectors( dx:Number, dy:Number ):Number
		{
			return Math.sqrt( ( dx * dx ) + ( dy * dy ) );
		}
		
		/** Returns the angle in degrees based on x and y vectors
		* 	@param 	dx 				Number. The x-position of the startpoint
		* 	@param 	dy 				Number. The y-position of the startpoint
		* 	@return					Number. The angle in degrees based on x and y vectors.
		*/
		// return angle in degrees based on x and y vectors
		public static function vectorsToAngle( dx:Number, dy:Number ):Number
		{
			return ( toDegrees( Math.atan2 ( dy, dx ) ) );
		}
		
		/** Returns the angle between 2 points
		* 	@param 	x1 				Number. The x-position of the startpoint
		* 	@param 	y1 				Number. The y-position of the startpoint
		* 	@param 	x2 				Number. The x-position of the endpoint
		* 	@param 	y2 				Number. The y-position of the endpoint
		* 	@return					Number. The angle between 2 points.
		*/
		public static function pointsToAngle( x1:Number, y1:Number, x2:Number, y2:Number ):Number
		{
			var dx:Number	= x2 - x1;
			var dy:Number	= y2 - y1;
			
			return vectorsToAngle( dx, dy );
		}
		
		/**
		 * Confines a numerical value between a minimum and a maximum.
		 * 
		 * @param	n:Number		The input value which is to be confined.
		 * @param	min:Number		The minimum value that the input value will need to have.
		 * @param	max:Number 		The maximum value that the input value will need to have.
		 * @return	Number			The confined value. 
		 */
		public static function confine ( n:Number, min:Number, max:Number ):Number
		{
			var confined:Number		= Math.max ( min, n );
			confined				= Math.min ( max, confined );
			return confined;
		}
		
		/**
		 * Rounds a numerical value to a given precision.
		 * 
		 * @param	n:Number			The input value which is to be rounded.
		 * @param	precision:Number	The precision value.
		 * @return	Number				The rounded value. 
		 */
		 
		public static function round ( n:Number, precision:int ):Number
		{
			var decimalPlaces:Number = Math.pow( 10, precision );
			return Math.round( decimalPlaces * n ) / decimalPlaces;
		}
	}
}